package com.naihe.anderson.sms.adapter;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.lang.ref.SoftReference;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.naihe.anderson.sms.R;
import com.naihe.anderson.sms.activity.PicShowActivity;
import com.naihe.anderson.sms.entity.RecordMoney;
import com.naihe.anderson.sms.util.FileUtil;

public class RecordInfoAdapter extends BaseAdapter{

    Context mContext;
    List<RecordMoney> objects;
    Date nowDate;
    
    private ConcurrentHashMap<String, SoftReference<Bitmap>> sSoftBitmapCache = new ConcurrentHashMap<String, SoftReference<Bitmap>>(5);

    public RecordInfoAdapter(Context context,List<RecordMoney> objects) {
        mContext = context;
        this.objects = objects;
        nowDate = new Date();
    }

    @Override
	public int getCount() {
		if(objects == null) return 0;
		return objects.size();
	}

	@Override
	public RecordMoney getItem(int position) {
		if(objects == null) return null;
		return objects.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}
	
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
        View view = null;
        if (convertView == null) {
            LayoutInflater inflater = LayoutInflater.from(mContext);
            view = inflater.inflate(R.layout.record_info_item, null);
        } else {
            view = convertView;
        }
        RecordMoney entityObj = objects.get(position);
        ImageView info_type_img = (ImageView) view.findViewById(R.id.info_type_img);
        if(entityObj.actionType == RecordMoney.ACTION_TYPE_EXPENDITURE){
        	info_type_img.setBackgroundResource(R.drawable.action_type_expenditure);
        } else {
        	info_type_img.setBackgroundResource(R.drawable.action_type_income);
        }
        ImageView projectpic_img = (ImageView) view.findViewById(R.id.projectpic_img);
        if(TextUtils.isEmpty(entityObj.projectPic)){
        	projectpic_img.setImageBitmap(null);
        	projectpic_img.setOnClickListener(null);
        } else {
        	SoftReference<Bitmap> bitmapReference = sSoftBitmapCache.get(entityObj.projectPic);
        	if(bitmapReference != null){
        		projectpic_img.setImageBitmap(bitmapReference.get());
        	} else {
        		Bitmap bitmap = getLoacalBitmap(entityObj.projectPic);
        		sSoftBitmapCache.put(entityObj.projectPic, new SoftReference<Bitmap>(bitmap));
        		projectpic_img.setImageBitmap(bitmap);
        	}
        	
        	projectpic_img.setTag(entityObj.projectPic);
        	projectpic_img.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					Intent intent = new Intent(mContext, PicShowActivity.class);
					intent.putExtra("bitmapName", (String)v.getTag());
					mContext.startActivity(intent);
				}
			});
        }
        ImageView map_img = (ImageView) view.findViewById(R.id.map_img);
        if(!TextUtils.isEmpty(entityObj.loation) && entityObj.loation.indexOf("/") > 0){
        	String mapName = entityObj.loation.substring(0, entityObj.loation.indexOf("/"));
        	SoftReference<Bitmap> bitmapReference = sSoftBitmapCache.get(entityObj.loation);
        	if(bitmapReference != null){
        		map_img.setImageBitmap(bitmapReference.get());
        	} else {
        		Bitmap bitmap = getLoacalBitmap(mapName);
        		sSoftBitmapCache.put(entityObj.loation, new SoftReference<Bitmap>(bitmap));
        		map_img.setImageBitmap(bitmap);
        	}
        	
        	map_img.setTag(mapName);
        	map_img.setOnClickListener(new OnClickListener() {
        		@Override
        		public void onClick(View v) {
        			Intent intent = new Intent(mContext, PicShowActivity.class);
        			intent.putExtra("bitmapName", (String)v.getTag());
        			mContext.startActivity(intent);
        		}
        	});
        } else {
        	map_img.setImageBitmap(null);
        	map_img.setOnClickListener(null);
        }
        final LinearLayout detial_layout = (LinearLayout) view.findViewById(R.id.detial_layout);
        ImageButton go_detial = (ImageButton)view.findViewById(R.id.go_detial);
        
        go_detial.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				if(!detial_layout.isShown()){
		        	detial_layout.setVisibility(View.VISIBLE);
		        	v.setBackgroundResource(R.drawable.main_fc_arrow_btn);
		        } else {
		        	v.setBackgroundResource(R.drawable.main_fc_arrow_point_down_btn);
		        	detial_layout.setVisibility(View.GONE);
		        }
			}
		});
        TextView info_number_et = (TextView) view.findViewById(R.id.info_number_et);
        info_number_et.setText("￥ " + entityObj.moneyNum);
        TextView info_project_et = (TextView) view.findViewById(R.id.info_project_et);
        info_project_et.setText(entityObj.project);
        TextView info_objectMember_et = (TextView) view.findViewById(R.id.info_objectMember_et);
        info_objectMember_et.setText(entityObj.objectMember);
        TextView info_remarks_et = (TextView) view.findViewById(R.id.info_remarks_et);
        info_remarks_et.setText(entityObj.remarks);
        TextView info_time = (TextView) view.findViewById(R.id.info_time);
        info_time.setText(getDataFormat(entityObj.createTime));

        return view;
    }
    
    private Bitmap getLoacalBitmap(String draw_name) {
		try {
			BitmapFactory.Options opt = new BitmapFactory.Options();
			opt.inSampleSize = 1;
			opt.inPreferredConfig = Bitmap.Config.RGB_565;
			opt.inPurgeable = true;
			opt.inInputShareable = true; // 获取资源图片
			FileInputStream fis = new FileInputStream(FileUtil.getPicNotePath() + draw_name);
			return BitmapFactory.decodeStream(fis, null, opt);
		} catch (FileNotFoundException e) {
			return null;
		}
	}
    
    private String getDataFormat(long time){
        String formatTime = "";
        Date date = new Date(time);
        try {
            if(date.getYear()==nowDate.getYear() && date.getMonth()==nowDate.getMonth() && date.getDate()==nowDate.getDate()){
				formatTime = mContext.getResources().getString(R.string.today) + new SimpleDateFormat("HH:mm").format(date);
			}else if(date.getYear()==nowDate.getYear() && date.getMonth()==nowDate.getMonth() && date.getDate()==(nowDate.getDate()-1)){
				formatTime = mContext.getResources().getString(R.string.yesterday) + new SimpleDateFormat("HH:mm").format(date);
			}else if(date.getYear()==nowDate.getYear()){
				formatTime = new SimpleDateFormat("MM-dd").format(date);
			}else{
				formatTime =  new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(date);
			}
        } catch (Exception e) {
            formatTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(date);;
        }
        return formatTime;
    }
    
    public void clear(){
    	sSoftBitmapCache.clear();
    	sSoftBitmapCache = null;
    }
}
